package com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.DingAdapter01;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.DingBean02;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.BaseUtils;
import com.lwj.liwanjia.utils.MyRecyclerView;
import com.lwj.liwanjia.utils.SpfUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/16.
 */

public class DingDanXiangQingHomeActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(DingDanXiangQingHomeActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;

                case 1:

                    if (homeList != null && homeList.size() >= 0) {
                        LinearLayoutManager manager = new LinearLayoutManager(DingDanXiangQingHomeActivity.this);
                        myRecyclerView.setLayoutManager(manager);
                        homeAdapter = new DingAdapter01(DingDanXiangQingHomeActivity.this, homeList);
                        myRecyclerView.setAdapter(homeAdapter);
                    }

                    if (!TextUtils.isEmpty(name) && !name.equals("null")) {
                        tv_name.setText(name);
                    }

                    if (!TextUtils.isEmpty(telephone)) {
                        tv_telephone.setText(telephone);
                    }

                    if (!TextUtils.isEmpty(address)) {
                        tv_address.setText(address);
                    }

                    if (!TextUtils.isEmpty(yingFu)) {
                        tv_yingFu.setText(yingFu);
                    }

                    if (!TextUtils.isEmpty(zongJia)) {
                        tv_zongJia.setText(zongJia);
                    }

                    if (!TextUtils.isEmpty(peiSongFangSHi)) {
                        tv_peiSongFangShi.setText(peiSongFangSHi);
                    }

                    if (!TextUtils.isEmpty(zhiFuFangSHi)) {
                        tv_zhiFuFangShi.setText(zhiFuFangSHi);
                    }

                    if (!TextUtils.isEmpty(dingDanBianHao)) {
                        tv_dingDanBianHao.setText(dingDanBianHao);
                    }

                    if (!TextUtils.isEmpty(xiaDanShiJian)) {
                        tv_xiaDanShiJian.setText(xiaDanShiJian);
                    }

                    if (!TextUtils.isEmpty(shenQingFang)) {
                        tv_shenQingFang.setText(shenQingFang);
                    }

                    if (!TextUtils.isEmpty(taiTou)) {
                        tv_taiTou.setText(taiTou);
                    }

                    if (!TextUtils.isEmpty(shiBieMa)) {
                        tv_shiBieMa.setText(shiBieMa);
                    }

                    if (!TextUtils.isEmpty(email)) {
                        tv_email.setText(email);
                    }

                    if (!TextUtils.isEmpty(liuYan)) {
                        tv_liuyan.setText(liuYan);
                    }

                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private TextView tv_zhuangTai;//状态
    private LinearLayout ll_bottom;//底部按钮
    private TextView tv_left;//底部 左边 按钮
    private TextView tv_right;//底部 右边 按钮
    private TextView tv_wuLiuChaXun;//物流查询
    private String dingDanState;//订单状态
    private MyRecyclerView myRecyclerView;
    private List<DingBean02.DataBean.CommodityBean> homeList;
    private DingAdapter01 homeAdapter;
    private String dingDanHao;//订单号


    private TextView tv_name;
    private TextView tv_telephone;
    private TextView tv_address;
    private TextView tv_zongJia;
    private TextView tv_yingFu;
    private TextView tv_dingDanBianHao;
    private TextView tv_xiaDanShiJian;
    private TextView tv_shenQingFang;
    private TextView tv_taiTou;
    private TextView tv_shiBieMa;
    private TextView tv_email;
    private TextView tv_liuyan;
    private String name;
    private String telephone;
    private String address;
    private String zongJia;//应付
    private String yingFu;//总价
    private TextView tv_peiSongFangShi;//配送方式
    private TextView tv_zhiFuFangShi;//支付方式
    private String dingDanBianHao;
    private String xiaDanShiJian;
    private String peiSongFangSHi;
    private String zhiFuFangSHi;
    private String shenQingFang;
    private String taiTou;
    private String shiBieMa;
    private String email;
    private String liuYan;
    private String photoUrl;//头像地址
    private String name1;//买家名字

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_zhuangTai = (TextView) findViewById(R.id.tv_zhuangTai);
        ll_bottom = (LinearLayout) findViewById(R.id.ll_bottom);
        tv_left = (TextView) findViewById(R.id.tv_left_bottom);
        tv_right = (TextView) findViewById(R.id.tv_right_bottom);
        tv_wuLiuChaXun = (TextView) findViewById(R.id.tv_wuLiuChaXun);
        myRecyclerView = (MyRecyclerView) findViewById(R.id.myRecyclerView);

        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_telephone = (TextView) findViewById(R.id.tv_telephone);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_zongJia = (TextView) findViewById(R.id.tv_zongJia);
        tv_yingFu = (TextView) findViewById(R.id.tv_yingFu);
        tv_dingDanBianHao = (TextView) findViewById(R.id.tv_dingDanBianHao);
        tv_xiaDanShiJian = (TextView) findViewById(R.id.tv_xiaDanShiJian);
        tv_peiSongFangShi = (TextView) findViewById(R.id.tv_peiSongFangShi);
        tv_zhiFuFangShi = (TextView) findViewById(R.id.tv_zhiFuFangShi);
        tv_shenQingFang = (TextView) findViewById(R.id.tv_shenQingFang);
        tv_shiBieMa = (TextView) findViewById(R.id.tv_shiBieMa);
        tv_email = (TextView) findViewById(R.id.tv_email);
        tv_liuyan = (TextView) findViewById(R.id.tv_liuYan);
        tv_taiTou = (TextView) findViewById(R.id.tv_taiTou);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("确认订单");

        tv_back.setOnClickListener(this);
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        tv_wuLiuChaXun.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        dingDanState = intent.getStringExtra("dingDanState");
        dingDanHao = intent.getStringExtra("dingDanHao");

        //根据状态 更改UI
        byStateChangeUI(dingDanState);

        //连接网络 获取数据
        initOkHttp();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_que_ren_ding_dan;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //底部 左边 按钮
            case R.id.tv_left_bottom:
                if (tv_left.getText().toString().equals("联系买家")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + telephone));
                    startActivity(intent);
                }
                break;
            //底部 右边 按钮
            case R.id.tv_right_bottom:
                if (tv_right.getText().toString().equals("发货")) {
                    Intent intent = new Intent(this, DaiFaHuoXiangQingActivity.class);
                    intent.putExtra("photoUrl", photoUrl);
                    intent.putExtra("name", name);
                    intent.putExtra("name1", name1);
                    intent.putExtra("telephone", telephone);
                    intent.putExtra("address", address);
                    intent.putExtra("homeList", (Serializable) homeList);
                    intent.putExtra("dingDanHao", dingDanHao);
                    startActivity(intent);
                    finish();
                }
                break;
            //物流查询
            case R.id.tv_wuLiuChaXun:
                Intent intent1 = new Intent(this, WuLiuActivity.class);
                intent1.putExtra("dingDanHao", dingDanHao);
                startActivity(intent1);
                break;

        }
    }

    //根据状态 更改UI
    private void byStateChangeUI(String dingDanState) {

        switch (dingDanState) {
            case "待发货":
                tv_title.setText("待发货订单");
                tv_zhuangTai.setText(dingDanState);
                ll_bottom.setVisibility(View.VISIBLE);
                break;
            case "待收货":
                tv_title.setText("已发货订单");
                tv_zhuangTai.setText("已付款");
                ll_bottom.setVisibility(View.VISIBLE);
                tv_right.setText("等待接收");
                tv_right.setEnabled(false);
                tv_right.setVisibility(View.VISIBLE);
                tv_wuLiuChaXun.setVisibility(View.VISIBLE);
                break;
            case "已完成":
                tv_title.setText("已完成订单");
                tv_zhuangTai.setText("已付款");
                ll_bottom.setVisibility(View.GONE);
                tv_wuLiuChaXun.setVisibility(View.VISIBLE);
                break;
            case "待付款":
                tv_title.setText("待付款订单");
                tv_zhuangTai.setText(dingDanState);
                break;
        }
    }

    //连接网络 获取数据
    private void initOkHttp() {

        String url = URLConstant.BASE_URL + "order/shopOneOrder";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("tradeId", dingDanHao)
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String result = response.body().string();
                    parse01(result);
                } catch (Exception e) {

                }
            }
        });
    }

    //解析
    private void parse01(String result) {

        Gson gson = new Gson();
        DingBean02 bean = gson.fromJson(result, DingBean02.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    homeList = new ArrayList<>();

                    for (int i = 0; i < bean.getData().getCommodity().size(); i++) {

                        DingBean02.DataBean.CommodityBean commodityBean = bean.getData().getCommodity().get(i);
                        homeList.add(commodityBean);
                    }

                    name = bean.getData().getConsignee().getConsigneeName();
                    telephone = bean.getData().getConsignee().getConsigneePhone();
                    address = bean.getData().getConsignee().getConsigneeAddress();
                    zongJia = "¥" + bean.getData().getTotalpay();
                    yingFu = "¥" + bean.getData().getTotalpay();
                    dingDanBianHao = bean.getData().getTradeId();


                    long time = bean.getData().getOrderTime();
                    //下单时间
                    xiaDanShiJian = BaseUtils.getString(String.valueOf(time / 1000));
                    zhiFuFangSHi = bean.getData().getPayWay();
                    peiSongFangSHi = bean.getData().getLogistics().getLogisticsType() + "";

                    shenQingFang = bean.getData().getLiwanjiaInvoice().getInvoiceProposer() + "";
                    taiTou = bean.getData().getLiwanjiaInvoice().getInvoiceTitle() + "";
                    shiBieMa = bean.getData().getLiwanjiaInvoice().getInvoiceTaxpayerCode() + "";
                    email = bean.getData().getLiwanjiaInvoice().getInvoiceMail() + "";


                    liuYan = bean.getData().getPayInfo() + "";
                    photoUrl = bean.getData().getHead();
                    name1 = bean.getData().getName();
                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }

}
